package net.asfun.jvalog.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.asfun.jvalog.domain.Config;
import net.asfun.jvalog.http.RequestUtil;

public class ConfigAction extends BaseAction{
	
	/**
	 * list site configs group by type
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	public void index(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		this.assign("configs", Config.listAll());
		this.render("admin/configs.html", resp);
	}
	
	/**
	 * view a config item
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	public void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		Long id = RequestUtil.getLongParam(req, "id");
		Config config = Config.load(id);
		this.assign("config", config);
		this.render("admin/config.html", resp);
	}
	
	/**
	 * change a config item
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		Long id = RequestUtil.getLongParam(req, "id");
		String value = RequestUtil.getRequiredParam(req, "value");
		Config conf = Config.load(id);
		conf.setValue(value);
		conf.update();
		this.feedback("config.update", req, resp);
	}
}
